package cn.ithers.blog.domain;

import lombok.Data;
import org.springframework.data.domain.Page;

import java.io.Serializable;
import java.util.List;

/**
 * @Author: zhanghuan
 * @date: 2020/10/16 9:20
 * @description:
 */

@Data
public class PageList<T> implements Serializable {

    //当前页码
    private long currentPage;
    //总数量
    private long totalCount;
    //每页有多少数量
    private long pageSize;
    //总页数
    private long totalPage;
    //是否第一页
    private boolean isFirst;
    //是否最后一页
    private boolean isLast;

    //数据
    private List<T> contents;

    public PageList() {

    }

    public PageList(long currentPage, long totalCount, long pageSize) {
        this.currentPage = currentPage;
        this.totalCount = totalCount;
        this.pageSize = pageSize;
        this.totalPage = this.totalCount / this.pageSize;
        //计算总的页数
        //是否第一页，是否最后以一个
        this.isFirst = this.currentPage == 1;
        this.isLast = this.currentPage == totalPage;
    }

    public void parsePage(Page<T> all) {
        setContents(all.getContent());
        setFirst(all.isFirst());
        setLast(all.isLast());
        setCurrentPage(all.getNumber() + 1);
        setTotalPage(all.getTotalElements());
        setTotalPage(all.getTotalPages());
        setPageSize(all.getSize());
    }
}
